package com.neu.flashmall.product.service.impl;

import com.neu.flashmall.product.entity.PmsSpuImages;
import com.neu.flashmall.product.feign.ThirdPartyFeignService;
import com.neu.flashmall.product.mapper.PmsSpuImagesMapper;
import com.neu.flashmall.product.service.PmsSpuImagesService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
@Service
public class PmsSpuImagesServiceImpl implements PmsSpuImagesService {

    @Resource
    private PmsSpuImagesMapper pmsSpuImagesMapper;

    @Resource
    private ThirdPartyFeignService thirdPartyFeignService;

    @Override
    public List<PmsSpuImages> getImagesBySpu(Long id) {
        return pmsSpuImagesMapper.getImageBySpu(id);
    }

    @Override
    public String updateUrl(String url, Long id) {
        PmsSpuImages data = new PmsSpuImages();
        data.setSpuId(id);
        data.setImgUrl(url);
        data.setImgName("null");
        data.setDefaultImg(0);
        int result = this.pmsSpuImagesMapper.insertSelective(data);
        if(result == 1) return "上传图片成功";
        return "更新URL失败";
    }

    @Override
    public String updateName(String name, Long id) {
        PmsSpuImages data = new PmsSpuImages();
        data.setId(id);
        data.setImgName(name);
        int result = this.pmsSpuImagesMapper.updateByPrimaryKeySelective(data);
        if(result == 1) return "更新成功";
        return "数据库异常";
    }

    public int deleteBySpuId(Long id){
        return pmsSpuImagesMapper.deleteBySpuId(id);
    }

    @Override
    public PmsSpuImages getDefaultImageBySpuId(Long spuId){
        return this.pmsSpuImagesMapper.selectDefaultImgBySpuId(spuId);
    }


    @Override
    @Transactional
    public String setDefault(Integer now, Long id,Long spuId) {
        if(now == 1){
            return "当前图片已经是默认图片了";
        }
        PmsSpuImages data = new PmsSpuImages();
        data.setId(id);
        data.setDefaultImg(1);
        PmsSpuImages d = this.pmsSpuImagesMapper.selectDefaultImgBySpuId(spuId);
        if(d == null){
            int result = this.pmsSpuImagesMapper.updateByPrimaryKeySelective(data);
            if(result == 1) return "已经将该图片设置为默认图片";
            return "数据库异常";
        }else{
            d.setDefaultImg(0);
            int result1 = this.pmsSpuImagesMapper.updateByPrimaryKeySelective(d);
            int result2 = this.pmsSpuImagesMapper.updateByPrimaryKeySelective(data);
            if(result1 == 1 && result2 == 1){
                return "已经将该图片设置为默认图片";
            }
            return "数据库异常";
        }
    }

    @Override
    @Transactional
    public String delete(Long id) {
        PmsSpuImages pmsSpuImages = this.pmsSpuImagesMapper.selectByPrimaryKey(id);
        if(pmsSpuImages == null) return "该图片不存在";
        String url = pmsSpuImages.getImgUrl();
        int result = pmsSpuImagesMapper.deleteByPrimaryKey(id);
        int code = thirdPartyFeignService.deleteSpuOneImage(url).getCode();
        if(result == 1 && code == 2000) return "删除成功";
        if(code != 2000) return "云服务器删除异常";
        return "数据库异常";
    }


}
